//
//  ZJQuestionsAnswersViewController.m
//  ShangHaiHuDongApp
//
//  Created by ZJ on 2017/6/22.
//  Copyright © 2017年 刘应平. All rights reserved.
//

#import "ZJQuestionsAnswersViewController.h"
#import "ZJQuestionAnTableViewCell.h"
#import "ZJRefurbish.h"
#import "ZJQADetailViewController.h"
#import "ZJQASharchView.h"
#import "ZJQAShareViewController.h"
#define CELLID @"ZJQuestionAnTableViewCell"
#import "LPWKWebViewController.h"
@interface ZJQuestionsAnswersViewController ()<UITableViewDelegate,UITableViewDataSource,ZJQASharchViewDelegate,UITextFieldDelegate>
/** tableView */
@property (nonatomic, strong) UITableView *tableView;
/** 数据 */
@property (nonatomic, strong) NSMutableArray *dataArray;
/** 刷新 */
@property (nonatomic, strong) ZJRefurbish *refurbish;
/** 搜索视图 */
@property (nonatomic, strong) ZJQASharchView *sharchView;
/**  分页  */
@property (assign, nonatomic) NSInteger  page;
@end

@implementation ZJQuestionsAnswersViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    if (self.headLine.length == 0) {
        self.titlelabel.text = @"i问答";
    }else{
        self.titlelabel.text = self.headLine;
    }
    
    [self.view addSubview:self.sharchView];
    [self.view addSubview:self.tableView];
    __weak typeof(self) weakSelf = self;
    [self.refurbish headerToScollview:self.tableView Block:^{
         weakSelf.page = 1;
        [weakSelf netWorking_header];
     }];
    
    [self.refurbish footerToScollview:self.tableView Block:^{
        weakSelf.page ++;
        [weakSelf netWorking_footer];
    }];;
    [self.tableView.mj_header beginRefreshing];
    
}

- (void)netWorking_header{
    NSString *urlString;
    NSMutableDictionary *paramer = [NSMutableDictionary dictionary];
    NSString *search = self.sharchView.textField.text.length != 0? self.sharchView.textField.text : @"";
    paramer[@"SELECT"] = search;
    paramer[@"PAGESTART"] = @"1";
    paramer[@"PAGECOUNT"] = @"15";
    paramer[@"GLOBALID"] = ZJUSERINFORMODEL.CURRENT_DEPT_MODEL.GLOBALID;
    if (self.headLine.length != 0) {
        paramer[@"TYPE"] = self.headLine;
        if (self.sharchView.textField.text.length != 0) {
            urlString = [NetWorkingURL solrFindQaForMobileSolr];
        }else{
            urlString = [NetWorkingURL qaActionSelectQaMbileTo];
        }
        
    }else{
         paramer[@"DEPT_ID"] = ZJUSERINFORMODEL.CURRENT_DEPT_MODEL.ID;
        if (self.sharchView.textField.text.length != 0) {
            paramer[@"SOLVE"] = @"2";
            urlString = [NetWorkingURL solrFindQAListForSolr];
        }else{
             paramer[@"SOLVE"] = @"";
            
            urlString = [NetWorkingURL qaActionQuestion];
        }
        
        
    }
        __weak typeof(self) weakSelf = self;
    [NetWorkingManager postWithUrlString:urlString Parameter:paramer Success:^(id object) {
        NetWorkModel *model = [NetWorkModel yy_modelWithJSON:object];
        if ([model.STATE isEqualToString:@"20000"]) {
            weakSelf.dataArray = [[NSArray yy_modelArrayWithClass:[ZJQuestionAnModel class] json:[object objectForKey:@"QUESTION_MEG"]] mutableCopy];
            [weakSelf.tableView reloadData];
        }else if ([model.STATE isEqualToString:@"50003"]){
            weakSelf.dataArray = [[NSArray yy_modelArrayWithClass:[ZJQuestionAnModel class] json:[object objectForKey:@"QUESTION_MEG"]] mutableCopy];
            [weakSelf.tableView reloadData];
            [YJProgressHUD showSuccess:object[@"ERROR_MSG"] inview:self.view];
        
        }
        [weakSelf.tableView.mj_header endRefreshing];
    } Fail:^(id object) {
        [weakSelf.tableView.mj_header endRefreshing];
    }];
}
- (void)netWorking_footer{
    NSString *urlString;
    NSMutableDictionary *paramer = [NSMutableDictionary dictionary];
    NSString *search = self.sharchView.textField.text.length != 0? self.sharchView.textField.text : @"";
    paramer[@"SELECT"] = search;
    paramer[@"PAGESTART"] = [NSString stringWithFormat:@"%ld",(long)_page];
    paramer[@"PAGECOUNT"] = @"15";
    paramer[@"GLOBALID"] = ZJUSERINFORMODEL.CURRENT_DEPT_MODEL.GLOBALID;
    if (self.headLine.length != 0) {
        paramer[@"TYPE"] = self.headLine;
        if (self.sharchView.textField.text.length != 0) {
            urlString = [NetWorkingURL solrFindQaForMobileSolr];
        }else{
            urlString = [NetWorkingURL qaActionSelectQaMbileTo];
        }
        
    }else{
        paramer[@"DEPT_ID"] = ZJUSERINFORMODEL.CURRENT_DEPT_MODEL.ID;
        if (self.sharchView.textField.text.length != 0) {
            paramer[@"SOLVE"] = @"2";
            urlString = [NetWorkingURL solrFindQAListForSolr];
        }else{
            paramer[@"SOLVE"] = @"";
            
            urlString = [NetWorkingURL qaActionQuestion];
        }
        
        
    }

    __weak typeof(self) weakSelf = self;
    [NetWorkingManager postWithUrlString:urlString Parameter:paramer Success:^(id object) {
        NetWorkModel *model = [NetWorkModel yy_modelWithJSON:object];
        if ([model.STATE isEqualToString:@"20000"]) {
            NSArray * array = [NSArray yy_modelArrayWithClass:[ZJQuestionAnModel class] json:[object objectForKey:@"QUESTION_MEG"]];
            [weakSelf.dataArray addObjectsFromArray:array];
            [weakSelf.tableView reloadData];
        }else if ([model.STATE isEqualToString:@"50003"]){
            [YJProgressHUD showSuccess:@"没有数据" inview:self.view];
            
        }
        [weakSelf.tableView.mj_footer endRefreshing];
    } Fail:^(id object) {
        [weakSelf.tableView.mj_footer endRefreshing];
    }];
}

#pragma mark - ZJQASharchViewDelegate
- (void)ZJQASharchView_delegate_search{
    if (self.sharchView.textField.text.length != 0) {
        [self.tableView.mj_header beginRefreshing];
    }
    

}


#pragma mark - UITextFiled 协议方法
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    
    
    NSString * toBeString = [textField.text stringByReplacingCharactersInRange:range withString:string];
    
    if (toBeString.length >= 50 && range.length!= 1 ){
        textField.text = [toBeString substringToIndex:50];
        return NO;
        
    }
    return YES;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    
    return YES;
}

- (void)textFieldDidEndEditing:(UITextField *)textField{
    [self.tableView.mj_header beginRefreshing];

}

#pragma mark - UITableView 协议方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ZJQuestionAnTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CELLID];
    cell.headLine = self.headLine;
    cell.model = self.dataArray[indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    ZJQuestionAnModel *model = self.dataArray[indexPath.row];
    return model.cellHeight;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
     ZJQuestionAnModel *model = self.dataArray[indexPath.row];
    if (self.headLine.length == 0) {
        ZJQADetailViewController *vc = [ZJQADetailViewController new];
        vc.model = model;
        [self.navigationController pushViewController:vc animated:YES];
    }else{
        /**  这里是跳web  */
        LPWKWebViewController *vc = [LPWKWebViewController new];
        vc.headLine = model.QUESTION;
        vc.urlString = model.URL;
        [self.navigationController pushViewController:vc animated:YES];
    }
    
}

#pragma mark - Setter
#pragma mark - Getters
- (UITableView *)tableView{
    if (_tableView == nil){
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, self.sharchView.bottom, self.view.width, self.view.height - self.sharchView.bottom) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView registerClass:[ZJQuestionAnTableViewCell class] forCellReuseIdentifier:CELLID];
    }
    return _tableView;
}
- (NSMutableArray *)dataArray{
    if (_dataArray == nil){
        _dataArray = [NSMutableArray array];
        
    }
    return _dataArray;
}

- (ZJRefurbish *)refurbish{
    if (_refurbish == nil){
        _refurbish = [ZJRefurbish new];
        
    }
    return _refurbish;
}
- (ZJQASharchView *)sharchView{
    if (_sharchView == nil){
        _sharchView = [[ZJQASharchView alloc]initWithFrame:CGRectMake(0, LP_TopHeight , self.view.width, 44 * [UIScreen zj_screenHScale])];
        _sharchView.delegate = self;
        _sharchView.textField.delegate = self;

    }
    return _sharchView;
}
@end
